<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在班级" prop="className">
        <el-select v-model="queryParams.className" clearable  placeholder="选择班级">
          <el-option
            v-for="item in classManageList"
            :key="item.id"
            :label="item.className"
            :value="item.className">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="teacherName">
        <el-select v-model="queryParams.className" clearable  placeholder="选择班级">
          <el-option
            v-for="item in classManageList"
            :key="item.id"
            :label="item.className"
            :value="item.className">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="困难系数" prop="hardLevel">
        <el-select v-model="queryParams.hardLevel" clearable  placeholder="请选择困难系数">
          <el-option
            v-for="item in hardLevel"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模拟面试" prop="mockTest">
        <el-select v-model="queryParams.mockTest" clearable  placeholder="请选择模拟面试结果">
          <el-option
            v-for="item in allTest"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结业面试" prop="endTest">
        <el-select v-model="queryParams.endTest" clearable  placeholder="请选择结业面试结果">
          <el-option
            v-for="item in allTest"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      留级界面是没有新增的，只能通过学生管理界面的关注按钮中添加留级-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:repeat:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:repeat:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:repeat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:repeat:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repeatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="留级管理ID" align="center" prop="id" />-->
      <el-table-column label="学生姓名" align="center" prop="studentName" />
<!--      <el-table-column label="班级ID" align="center" prop="classId" />-->
      <el-table-column label="班级名" align="center" prop="className" />
      <el-table-column label="教师姓名" align="center" prop="teacherName" />
      <el-table-column label="困难系数" align="center" prop="hardLevel" />
      <el-table-column label="模拟面试" align="center" prop="mockTest" />
      <el-table-column label="结业面试" align="center" prop="endTest" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:repeat:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:repeat:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改留级管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
            <el-select v-model="form.studentName" clearable  placeholder="请选择学生">
              <el-option
                v-for="item in studentManageList"
                :key="item.id"
                :label="item.studentName"
                :value="item.studentName">
              </el-option>
            </el-select>
        </el-form-item>
<!--        <el-form-item label="班级ID" prop="classId">-->
<!--          <el-input v-model="form.classId" placeholder="请输入班级ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="所在班级" prop="className">
          <el-select v-model="form.className" clearable  placeholder="请选择所在班级">
            <el-option
              v-for="item in classManageList"
              :key="item.id"
              :label="item.className"
              :value="item.className">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
        </el-form-item>
        <el-form-item label="困难系数" prop="hardLevel">
          <el-select v-model="form.hardLevel" clearable  placeholder="请选择困难系数">
            <el-option
              v-for="item in hardLevel"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="mockTest">
          <el-select v-model="form.mockTest" clearable  placeholder="请选择模拟面试结果">
            <el-option
              v-for="item in allTest"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业面试" prop="endTest">
          <el-select v-model="form.endTest" clearable  placeholder="请选择结业面试结果">
            <el-option
              v-for="item in allTest"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepeat, getRepeat, delRepeat, addRepeat, updateRepeat } from "@/api/system/repeat";
import {getDicts} from "@/api/system/dict/data";
import {listClassManage} from "@/api/system/classManage";
import {listStudentManage} from "@/api/system/studentManage";

export default {
  name: "Repeat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留级管理表格数据
      repeatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        classId: null,
        className: null,
        teacherName: null,
        hardLevel: null,
        mockTest: null,
        endTest: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      studentManageList:{},
      classManageList:{},
      allTest:[],
      hardLevel:[],
    };
  },
  created() {

    this.getList();

    this.getStudentList();

    this.getClassList();

    getDicts("test_status").then(result => {
      this.allTest = result.data;
    });
    getDicts("hard_level").then(result => {
      this.hardLevel = result.data;
    });
  },
  methods: {
    /** 当选择班级的选择框 发生改变时 触发 查询班级里面的学生*/
    getStudentList() {
      listStudentManage(this.queryParams).then(response => {
        this.studentManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** */
    getClassList() {
      this.loading = true;
      listClassManage(this.queryParams).then(response => {
        this.classManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询留级管理列表 */
    getList() {
      this.loading = true;
      listRepeat(this.queryParams).then(response => {
        this.repeatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        classId: null,
        className: null,
        teacherName: null,
        hardLevel: null,
        mockTest: null,
        endTest: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加留级管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepeat(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改留级管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepeat(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepeat(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除留级管理编号为"' + ids + '"的数据项？').then(function() {
        return delRepeat(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/repeat/export', {
        ...this.queryParams
      }, `repeat_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
